package com.chenglu.android.redpacket.mvp.presenter;





import com.chenglu.android.bean.SmsCodeBean;
import com.chenglu.android.common.base.BasePresenter;
import com.chenglu.android.common.net.Constant;
import com.chenglu.android.common.net.RxScheduler;
import com.chenglu.android.common.net.exception.NetErrorException;
import com.chenglu.android.common.utils.ToastUtils;
import com.chenglu.android.mine.bean.CollectBean;
import com.chenglu.android.redpacket.bean.OpenPacketBean;
import com.chenglu.android.redpacket.bean.PacketBean;
import com.chenglu.android.redpacket.mvp.contract.RedPacketContract;
import com.chenglu.android.redpacket.mvp.model.RedPacketModel;

import java.util.Map;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import okhttp3.RequestBody;


/**
 * @author: lyj
 * @Created by Copyright 2023/7/27 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 *
 */
public class RedPacketPresenter extends BasePresenter<RedPacketContract.View> implements RedPacketContract.RedPacketPresenter {

     private  RedPacketContract.RedPacketModel model;

    public RedPacketPresenter(){
        model = new RedPacketModel();
    }


    @Override
    public void getOpenPacket(String token,String id) {
        if(!isViewAttached()){
            return;
        }
        model.getOpenPacket(token, id)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<OpenPacketBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
//                        LogUtils.e("qqqqqqq:"+"onSubscribe");
                    }

                    @Override
                    public void onNext(@NonNull OpenPacketBean bean) {
//                        LogUtils.e("qqqqqqq:"+"onNext");

//                        mView.getUserInfoSuccess(bean);
                        if(bean.getCode()==Constant.HTTPCODE200){
                            mView.getOpenPacket(bean);
                        }else {
                            ToastUtils.show(bean.getMsg());
                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
//                        LogUtils.e("qqqqqqq:"+"onComplete");

                    }
                });
    }

    @Override
    public void getPacketList(Map<String, String> map) {
        if(!isViewAttached()){
            return;
        }
        model.getPacketList(map)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<PacketBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
//                        LogUtils.e("qqqqqqq:"+"onSubscribe");
                    }

                    @Override
                    public void onNext(@NonNull PacketBean bean) {
//                        LogUtils.e("qqqqqqq:"+"onNext");

//                        mView.getUserInfoSuccess(bean);
                        if(bean.getCode()==Constant.HTTPCODE200){
                            mView.getPacketList(bean);
                        }else {
                            ToastUtils.show(bean.getMsg());
                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
//                        LogUtils.e("qqqqqqq:"+"onComplete");

                    }
                });
    }

    @Override
    public void getPacketList(String token, Map<String, String> map) {
        if(!isViewAttached()){
            return;
        }
        model.getPacketList(token,map)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<PacketBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
//                        LogUtils.e("qqqqqqq:"+"onSubscribe");
                    }

                    @Override
                    public void onNext(@NonNull PacketBean bean) {
//                        LogUtils.e("qqqqqqq:"+"onNext");

//                        mView.getUserInfoSuccess(bean);
                        if(bean.getCode()==Constant.HTTPCODE200){
                            mView.getPacketList(bean);
                        }else {
                            ToastUtils.show(bean.getMsg());
                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
//                        LogUtils.e("qqqqqqq:"+"onComplete");

                    }
                });
    }


}
